package cn.wb.org.controller;

import cn.wb.base.anno.PreAuthorize;
import cn.wb.base.util.AjaxResult;
import cn.wb.base.util.PageList;
import cn.wb.org.domain.Department;
import cn.wb.org.query.DepartmentQuery;
import cn.wb.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController  // @Controller + @ResponseBody
@RequestMapping(value = "/dept")
@Api(value = "部门的API", description = "部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

//    /**
//     * 之前定义接口的玩法
//     * @return
//     */
//    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
//    public List<Department> findAll(){
//        return departmentService.findAll();
//    }

    /**
     * restful、 风格
     */
    @GetMapping
    public List<Department> findAll() {
        return departmentService.findAll();
    }

//    /**
//     * restful 风格 路径变量
//     *  如果前端传的参数名称 叫 deptId
//      http://localhost:8080/findById? id = 1
//     */
//    @RequestMapping("/findById")
//    public Department findById(@RequestParam(value = "deptId",required = true) Long id){
//        return departmentService.findById(id);
//    }

    /**
     * restful 风格 路径变量
     * 如果前端传的参数名称 叫 deptId
     * http://localhost:8080/dept/1
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public Department findById(@ApiParam(value = "ID属性", required = true)
                               @PathVariable("id") Long id) {
        return departmentService.findById(id);
    }

    /**
     * 添加/ 修改
     *
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        if (null == department.getId()) {
            // 添加
            departmentService.add(department);
        } else {
            // 编辑
            departmentService.update(department);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        departmentService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 分页 json格式交互 字符串
     *
     * @RequestBody 接收前端的json数据 转成我们的实体
     */
    @PostMapping
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery quey) {

        return departmentService.queryByPage(quey);
    }

    /**
     * 批量删除
     */
    @PatchMapping
    @PreAuthorize(name = "删除部门批量",sn="department:batchDel")
    public AjaxResult batchDetel(@RequestBody List<Long> ids) {
        departmentService.batchDetel(ids);
        return AjaxResult.me().success();
    }

    @GetMapping("/tree")
    @PreAuthorize(name = "查询部门级数",sn="department:tree")
    public List<Department> deptTree() {
        return departmentService.deptTree();
    }

}
